<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
    <meta charset="utf-8">
    <title>告警记录</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="./layui/css/layui.css" th:href="@{/layui/css/layui.css}" media="all">
    <link rel="stylesheet" href="./style/admin.css" th:href="@{/style/admin.css}" media="all">
    <link rel="stylesheet" href="./style/myself.css" th:href="@{/style/myself.css}" media="all">
    <style type="text/css">
        /* 让layui的table表格在同一单元格换行显示 */
        .layui-table-cell {
            height: auto;
        }
    </style>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="./js/html5.min.js" th:src="@{/js/html5.min.js}"></script>
    <script src="./js/respond.min.js" th:src="@{/js/respond.min.js}"></script>
    <![endif]-->
    <script type="text/javascript" charset="utf-8" th:inline="javascript">
        var ctxPath = /*[[@{/}]]*/'';
        var authority = [[${#authorization.expression('hasAuthority("超级管理员")')}]];
    </script>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <form class="layui-form-item" onsubmit="return false;">
                <div class="layui-inline">
                    <label class="layui-form-label">告警类型</label>
                    <div class="layui-input-block" style="width: 182px;">
                        <select name="type">
                            <option value="">所有</option>
                            <option value="SERVER">服务器</option>
                            <option value="INSTANCE">应用</option>
                            <option value="DATABASE">数据库</option>
                            <option value="NET">网络</option>
                            <option value="TCP4SERVICE">TCP服务</option>
                            <option value="HTTP4SERVICE">HTTP服务</option>
                            <option value="CUSTOM">自定义</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">告警级别</label>
                    <div class="layui-input-block" style="width: 182px;">
                        <select name="level">
                            <option value="">所有</option>
                            <option value="INFO">消息</option>
                            <option value="WARN">警告</option>
                            <option value="ERROR">错误</option>
                            <option value="FATAL">严重</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">告警状态</label>
                    <div class="layui-input-block" style="width: 182px;">
                        <select name="status">
                            <option value="">所有</option>
                            <option value="1">成功</option>
                            <option value="0">失败</option>
                            <option value="-1">不提醒</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">告警标题</label>
                    <div class="layui-input-block">
                        <input type="text" name="title" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <br>
                <div class="layui-inline">
                    <label class="layui-form-label">告警内容</label>
                    <div class="layui-input-block">
                        <input type="text" name="content" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">号码</label>
                    <div class="layui-input-block">
                        <input type="text" name="number" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">记录日期</label>
                    <div class="layui-input-block">
                        <input type="text" class="layui-input" id="insertDate" name="insertDate" autocomplete="off"
                               placeholder="yyyy-MM-dd">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">告警日期</label>
                    <div class="layui-input-block">
                        <input type="text" class="layui-input" id="updateDate" name="updateDate" autocomplete="off"
                               placeholder="yyyy-MM-dd">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit
                            lay-filter="alarm-record-list-table-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                    <button id="reset" type="reset" class="layui-btn">清空</button>
                    <button id="export" class="layui-btn" lay-submit
                            lay-filter="alarm-record-list-table-export">导出
                    </button>
                </div>
            </form>
        </div>

        <div class="layui-card-body">
            <table id="alarm-record-list-table" lay-filter="alarm-record-list-table"></table>
            <script type="text/html" id="imgTpl">
                <img style="display: inline-block; width: 50%; height: 100%;" src={{ d.avatar }}>
            </script>
            <script type="text/html" id="list-table-toolbar">
                <div class="layui-btn-group" sec:authorize="hasAuthority('超级管理员')">
                    <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="batchdel">
                        <i class="layui-icon layui-icon-delete"></i>删除
                    </button>
                    <button class="layui-btn layui-btn-sm layui-btn-clear" lay-event="cleanup">
                        <i class="layui-icon layui-icon-delete"></i>清空
                    </button>
                </div>
            </script>
            <script type="text/html" id="alarm-record-list-table-toolbar">
                <div class="layui-btn-group">
                    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="info">
                        <i class="layui-icon layui-icon-about"></i>详情</a>
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"
                       sec:authorize="hasAuthority('超级管理员')">
                        <i class="layui-icon layui-icon-delete"></i>删除</a>
                </div>
            </script>
        </div>
    </div>
</div>
<script src="./js/common.js" th:src="@{/js/common.js}" charset="utf-8"></script>
<script src="./layui/layui.js" th:src="@{/layui/layui.js}"></script>
<script charset="utf-8" th:inline="javascript">
    layui.config({
        base: ctxPath //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'laydate'], function () {
        var $ = layui.$, admin = layui.admin, form = layui.form, table = layui.table, device = layui.device(),
            laydate = layui.laydate;
        //日期选择器
        laydate.render({
            elem: '#insertDate'
            //,type: 'date' //type 默认为 date，所以可不填
        });
        //日期选择器
        laydate.render({
            elem: '#updateDate'
            //,type: 'date' //type 默认为 date，所以可不填
        });
        table.render({
            elem: '#alarm-record-list-table',
            url: ctxPath + 'monitor-alarm-record/get-monitor-alarm-record-list',
            toolbar: '#list-table-toolbar',
            request: {
                pageName: 'current',//页码的参数名称，默认：page
                limitName: 'size' //每页数据量的参数名，默认：limit
            },
            response: {
                statusName: 'code', //规定数据状态的字段名称，默认：code
                statusCode: 200,//规定成功的状态码，默认：0
                msgName: 'msg',//规定状态信息的字段名称，默认：msg
                countName: 'count', //规定数据总数的字段名称，默认：count
                dataName: 'data' //规定数据列表的字段名称，默认：data
            },
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    'code': res.code, //解析接口状态
                    'msg': res.msg, //解析提示文本
                    'count': res.data.total, //解析数据长度
                    'data': res.data.records //解析数据列表
                };
            },
            done: function () {
                $('[data-field="type"]').children().each(function () {
                    if ($(this).text() === 'SERVER') {
                        $(this).text('服务器');
                    }
                    if ($(this).text() === 'NET') {
                        $(this).text('网络');
                    }
                    if ($(this).text() === 'TCP4SERVICE') {
                        $(this).text('TCP服务');
                    }
                    if ($(this).text() === 'HTTP4SERVICE') {
                        $(this).text('HTTP服务');
                    }
                    if ($(this).text() === 'INSTANCE') {
                        $(this).text('应用');
                    }
                    if ($(this).text() === 'CUSTOM') {
                        $(this).text('自定义');
                    }
                    if ($(this).text() === 'DATABASE') {
                        $(this).text('数据库');
                    }
                });
                $('[data-field="level"]').children().each(function () {
                    if ($(this).text() === 'INFO') {
                        $(this).text('消息');
                    }
                    if ($(this).text() === 'WARN') {
                        $(this).text('警告');
                    }
                    if ($(this).text() === 'ERROR') {
                        $(this).text('错误');
                    }
                    if ($(this).text() === 'FATAL') {
                        $(this).text('严重');
                    }
                });
                $('[data-field="status"]').children().each(function () {
                    if ($(this).text() === '0') {
                        $(this).html('<span style="color: #FF4500;font-weight: bold;">失败</span>');
                    }
                    if ($(this).text() === '1') {
                        $(this).text('成功');
                    }
                    if ($(this).text() === '' || $(this).text() === null) {
                        $(this).text('不提醒');
                    }
                });
                $('[data-field="way"]').children().each(function () {
                    $(this).html($(this).text()
                        .replace(new RegExp('MAIL', 'g'), '邮件')
                        .replace(new RegExp('SMS', 'g'), '短信')
                        .replace(new RegExp(';', 'g'), '<br>'));
                });
                $('[data-field="number"]').children().each(function () {
                    $(this).html($(this).text().replace(new RegExp(';', 'g'), '<br>'));
                });
            },
            cols: [
                [{
                    type: 'checkbox',
                    hide: !authority
                }, {
                    field: 'id',
                    width: 80,
                    title: 'ID',
                    sort: !0,
                    hide: true
                }, {
                    field: 'type',
                    width: 110,
                    title: '告警类型',
                    sort: !0
                }, {
                    field: 'way',
                    width: 110,
                    title: '告警方式',
                    sort: !0
                }, {
                    field: 'level',
                    width: 110,
                    title: '告警级别',
                    sort: !0
                }, {
                    field: 'title',
                    title: '告警标题',
                    width: 120,
                    sort: !0
                }, {
                    field: 'content',
                    minWidth: 400,
                    title: '告警内容',
                    sort: !0
                }, {
                    field: 'status',
                    width: 80,
                    title: '状态',
                    sort: !0
                }, {
                    field: 'number',
                    width: 120,
                    title: '号码',
                    sort: !0
                }, {
                    field: 'insertTime',
                    minWidth: 170,
                    title: '记录时间',
                    sort: !0
                }, {
                    field: 'updateTime',
                    minWidth: 170,
                    title: '告警时间',
                    sort: !0
                }, {
                    title: '操作',
                    width: 140,
                    align: 'center',
                    //fixed: (device.ios || device.android) ? false : 'right',
                    fixed: false,
                    toolbar: '#alarm-record-list-table-toolbar'
                }]
            ],
            page: !0,
            limit: 15,
            limits: [10, 15, 20, 30, 40, 50, 60, 70, 80, 90, 100],
            height: 'full-180'
        });
        //监听搜索
        form.on('submit(alarm-record-list-table-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('alarm-record-list-table', {
                where: field
            });
        });
        // 监听导出
        form.on('submit(alarm-record-list-table-export)', function (data) {
            var field = data.field;
            var type = field.type;
            var level = field.level;
            var status = field.status;
            var content = field.content;
            var title = field.title;
            var number = field.number;
            var insertDate = field.insertDate;
            var updateDate = field.updateDate;
            window.location.href = ctxPath + 'monitor-alarm-record/export-monitor-alarm-record-list?' +
                'content=' + content + '&level=' + level + '&status=' + status + '&title=' + title +
                '&type=' + type + '&number=' + number + '&insertDate=' + insertDate + '&updateDate=' + updateDate;
        });
        // 点击表头排序
        table.on('sort(alarm-record-list-table)', function (obj) {
            table.reload('alarm-record-list-table', {
                initSort: obj
            });
        });
        //头工具栏事件
        table.on('toolbar(alarm-record-list-table)', function (obj) {
            switch (obj.event) {
                case 'batchdel':
                    var checkStatus = table.checkStatus('alarm-record-list-table'), checkData = checkStatus.data; //得到选中的数据
                    if (checkData.length === 0) {
                        return layer.msg('请选择数据');
                    }
                    layer.confirm('确定删除吗？', function (index) {
                        admin.req({
                            type: 'delete',
                            url: ctxPath + 'monitor-alarm-record/delete-monitor-alarm-record',
                            data: JSON.stringify(checkStatus.data),
                            dataType: 'json',
                            contentType: 'application/json;charset=utf-8',
                            headers: {
                                [[${_csrf.headerName}]]: [[${_csrf.token}]]
                            },
                            success: function (result) {
                                var data = result.data;
                                if (data === webConst.SUCCESS) {
                                    table.reload('alarm-record-list-table'); //数据刷新
                                    layer.msg('删除成功！', {icon: 6});
                                } else {
                                    layer.msg('删除失败！', {icon: 5, shift: 6});
                                }
                            },
                            error: function () {
                                layer.msg('系统错误！', {icon: 5, shift: 6});
                            }
                        });
                    });
                    break;
                case 'cleanup':
                    layer.confirm('确定清空吗？', function (index) {
                        admin.req({
                            type: 'delete',
                            url: ctxPath + 'monitor-alarm-record/cleanup-monitor-alarm-record',
                            dataType: 'json',
                            contentType: 'application/json;charset=utf-8',
                            headers: {
                                [[${_csrf.headerName}]]: [[${_csrf.token}]]
                            },
                            success: function (result) {
                                var data = result.data;
                                if (data === webConst.SUCCESS) {
                                    table.reload('alarm-record-list-table'); //数据刷新
                                    layer.msg('清空成功！', {icon: 6});
                                } else {
                                    layer.msg('清空失败！', {icon: 5, shift: 6});
                                }
                            },
                            error: function () {
                                layer.msg('系统错误！', {icon: 5, shift: 6});
                            }
                        });
                    });
                    break;
            }
        });
        //监听工具条
        table.on('tool(alarm-record-list-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('确定删除吗？', function (index) {
                    admin.req({
                        type: 'delete',
                        url: ctxPath + 'monitor-alarm-record/delete-monitor-alarm-record',
                        data: JSON.stringify([data]),
                        dataType: 'json',
                        contentType: 'application/json;charset=utf-8',
                        headers: {
                            [[${_csrf.headerName}]]: [[${_csrf.token}]]
                        },
                        success: function (result) {
                            var data = result.data;
                            if (data === webConst.SUCCESS) {
                                obj.del();
                                table.reload('alarm-record-list-table'); //数据刷新
                                layer.msg('删除成功！', {icon: 6});
                            } else {
                                layer.msg('删除失败！', {icon: 5, shift: 6});
                            }
                        },
                        error: function () {
                            layer.msg('系统错误！', {icon: 5, shift: 6});
                        }
                    });
                });
            }
            if (obj.event === 'info') {
                // 宽度
                var width;
                // 高度
                var height;
                // 移动端
                if (device.ios || device.android) {
                    // 宽度
                    width = document.body.clientWidth * 0.8 + 'px';
                    // 高度
                    height = document.body.clientHeight * 0.55 + 'px';
                } else {
                    // 宽度
                    width = document.body.clientWidth * 0.8 + 'px';
                    // 高度
                    height = document.body.clientHeight * 0.8 + 'px';
                }
                var infoLayerIndex = layer.open({
                    type: 2,
                    title: '告警记录详情',
                    content: ctxPath + 'monitor-alarm-record/monitor-alarm-record-detail?id=' + data.id,
                    maxmin: true,
                    shade: 0,
                    moveOut: true,
                    area: [width, height],
                    success: function (layero, index) {
                        // 在回调方法中的第2个参数“index”表示的是当前弹窗的索引
                        // 通过layer.full方法将窗口放大
                        // layer.full(index);
                    }
                });
                layer.full(infoLayerIndex);
            }
        });
    });
</script>
</body>
</html>